package com.shanghaionstar.vehicle_ownerlife;

import android.app.DatePickerDialog;
import android.content.Context;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.TextView;

import org.joda.time.Days;
import org.joda.time.LocalDate;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 日期辅助类
 * <p>
 * Created by Yonglang Hong on 2017/1/5.
 */
public class DateHelper {

    public static void setDate(Context context, final TextView text) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int montyOfYear, int dayOfMonth) {
                StringBuilder sb = new StringBuilder();
                sb.append(String.format(Locale.getDefault(), "%d-%02d-%02d",
                        year,
                        montyOfYear + 1,
                        dayOfMonth));
                text.setText(sb);
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    public static String updateDateYear(String inputDate, int years){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = null;
        try {
            Date date = dateFormat.parse(inputDate);
            calendar.setTimeInMillis(date.getTime());
            calendar.add(Calendar.YEAR,years);
            date = new Date(calendar.getTimeInMillis());
            dateStr = dateFormat.format(date);
        } catch (Exception ex) {
        }
        return dateStr;
    }

    public static void setDate(Context context, DatePickerDialog.OnDateSetListener dateSetListener) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, dateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    /**
     * 输入日期与当前日期比较后的值在输入年份内
     *
     * @param inputDate 输入日期
     * @param years     年份范围
     * @return
     */
    public static boolean isDateInScope(String inputDate, int years) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        try {
            Date dateInput = dateFormat.parse(inputDate);
            LocalDate d1 = new LocalDate();
            LocalDate d2 = new LocalDate(dateInput.getTime());
            int days = Math.abs(Days.daysBetween(d2, d1).getDays());
            if (days >=0 && days <= 365 * years) {
                return true;
            }
        } catch (Exception ex) {
            return false;
        }
        return false;
    }

    /**
     * 获取相差天数
     * @param inputDate
     * @return
     */
    public static int getBetweenDays(String inputDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        try {
            Date dateInput = dateFormat.parse(inputDate);
            LocalDate d1 = new LocalDate();
            LocalDate d2 = new LocalDate(dateInput.getTime());
            int days = Math.abs(Days.daysBetween(d1, d2).getDays());
            return days;
        } catch (Exception ex) {
            return 0;
        }
    }

    /**
     * 日期前后比较
     * @param inputDate
     * @return
     */
    public static boolean nowIsBefore(String inputDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        try {
            Date dateInput = dateFormat.parse(inputDate);
            Date now = new Date();
            String nowString = dateFormat.format(now);
            now = dateFormat.parse(nowString);
            return now.getTime() <= dateInput.getTime();
        } catch (Exception ex) {
            return false;
        }
    }

    public static int currentHour(){
        int hour = 9;
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        Log.d("currentHour",hour + "");
        return hour;
    }

}
